package com.hualan.sfzyproject.controller;

import com.hualan.sfzyproject.domain.Announcement;
import com.hualan.sfzyproject.service.AnnouncementService;
import com.hualan.sfzyproject.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/announcement")
public class AnnouncementController {

    @Autowired
    private AnnouncementService announcementService;

    //查询所有公告及发布的咨询师    前台
    @GetMapping
    public R selectAll() {
        return R.ok(announcementService.selectAll());
    }

    //根据咨询师id查询公告  后台
    @GetMapping("/selectByConsultantId")
    public R selectByConsultantId(Long consultantId) {
        return R.ok(announcementService.selectByConsultantId(consultantId));
    }

    //添加公告
    @PostMapping
    public R insert(Announcement announcement) {
        if (announcementService.insert(announcement) > 0){
            return R.ok("添加成功");
        }else{
            return R.error("添加失败");
        }
    }

    //删除公告
    @DeleteMapping
    public R delete(Long id) {
        if (announcementService.delete(id) > 0){
            return R.ok("删除成功");
        }else{
            return R.error("删除失败");
        }
    }

    //修改公告
    @PutMapping
    public R update(@RequestBody Announcement announcement) {
        System.out.println("修改公告: " + announcement);
        if (announcementService.update(announcement) > 0){
            return R.ok("修改成功");
        }else{
            return R.error("修改失败");
        }
    }

    //根据id查询公告
    @GetMapping("/selectById")
    public R selectById(Long id) {
        System.out.println("根据id查询公告: " + id);
        return R.ok(announcementService.selectById(id));
    }


}
